//
//  Geofence.m
//  why
//
//  Created by toogps on 2019/5/31.
//  Copyright © 2019 apple. All rights reserved.
//

#import "Geofence.h"

@implementation Geofence

//单例
+ (instancetype)shareLocation {
    static Geofence *location = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        location = [[Geofence alloc]init];
    });
    return location;
}

//开始定位
- (void)getLocation{
    // 开始定位
    [self.locationManager startUpdatingLocation];
}

#pragma mark -- CLLocationManagerDelegate协议
//获取当前定位位置信息
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    [self.locationManager stopUpdatingLocation];
    //  获取当前位置信息
    CLLocation *locationC = locations.lastObject;
    
    //更新地理围栏中心
    [self UpDateCircular:locationC.coordinate.latitude longitude:locationC.coordinate.longitude];
    CLLocationCoordinate2D sss = CLLocationCoordinate2DMake(locationC.coordinate.latitude, locationC.coordinate.longitude);
    
    self.latitude = sss.latitude;
    self.longitude = sss.longitude;
    if (self.block) {
        self.block(self.longitude, self.latitude);
    }
    [self uploadLocation];
}

//当定位发生错误时调用
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"定位错误原因:%@",error);
}

//网络请求,上传自己的地理位置
-(void)uploadLocation {
    
}

//检查定位权限
-(void)checkLocationPermissions{
    if ([CLLocationManager locationServicesEnabled] && ( [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways)) {
        //创建通知
        NSNotification *notification = [NSNotification notificationWithName:@"haveLocation" object:nil userInfo:nil];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        //定位功能可用
    }else{
        //创建通知
        NSNotification *notification = [NSNotification notificationWithName:@"haveNotLocation" object:nil userInfo:nil];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }
}

// 根据当前位置重新画圆
-(void)UpDateCircular:(double)latitude longitude:(double)longitude{
    if ([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]){
        CLLocationCoordinate2D companyCenter;
        companyCenter.latitude = latitude;
        companyCenter.longitude = longitude;
        // 使用CLCircularRegion创建圆形区域，半径100米
        self.fkit = nil;
        self.fkit = [[CLCircularRegion alloc] initWithCenter:companyCenter radius:100 identifier:@"MDZZ"];
        //监测这个区域
        [self.locationManager startMonitoringForRegion:self.fkit];
        //进入/离开区域调用协议方法
        [self.locationManager requestStateForRegion:self.fkit];
    }else{
        NSLog(@"不能用");
    }
    
}

//进入了围栏
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"进入围栏的属性：%@",region);
    [self getLocation];
    
}
//离开了围栏
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
    NSLog(@"离开围栏的属性：%@",region);
    [self getLocation];
}


#pragma mark -- 懒加载
-(CLLocationManager *)locationManager{
    if (!_locationManager) {
        // 初始化定位管理器
        _locationManager = [[CLLocationManager alloc] init];
        // 设置代理
        _locationManager.delegate = self;
        // 设置定位精确度到米
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 设置过滤器为无
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        //这句话ios8以上版本使用。
        [_locationManager requestAlwaysAuthorization];
        //运行后台定位
        _locationManager.allowsBackgroundLocationUpdates = YES;
        //禁止App暂停定位
        _locationManager.pausesLocationUpdatesAutomatically = NO;
        [_locationManager startMonitoringSignificantLocationChanges];
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkLocationPermissions) userInfo:nil repeats:YES];
    }
    return _locationManager;
}
@end
